package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.vo.fx.VKUserInfoVO;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.userms.dto.ShopVipUserDTO;
import com.yuanfeng.userms.entity.UserVipEntity;
import com.yuanfeng.userms.mapper.UserVipMapper;
import com.yuanfeng.userms.mapper.UserVkMapper;
import com.yuanfeng.userms.service.UserVipService;
import com.yuanfeng.userms.vo.UserYgInfoVO;
import com.yuanfeng.userms.vo.VipUserListVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserVipServiceImpl
 * @Description 店铺会员
 * @Author liulh
 * @Date 2023/8/8 16:05
 * @Version 1.0
 **/
@Service
public class UserVipServiceImpl extends ServiceImpl<UserVipMapper, UserVipEntity>
        implements UserVipService {

    @Resource
    private UserVkMapper userVkMapper;

    @Override
    public ResponseResult queryShopVipUser(ShopVipUserDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.getToken());
        Page<VipUserListVO> page = new Page<>(param.getPage(), param.getLimit());
        param.setShopId(userInfo.getShopId() == null ? null : Integer.parseInt(userInfo.getShopId()));
        IPage<VipUserListVO> vipUserList = this.baseMapper.queryShopNoVkVipUser(page, param);
        return ResponseResult.success(PageUtils.getPage(vipUserList));
    }

    @Override
    public ResponseResult editShopVipUser(ShopVipUserDTO param) {
        int result = this.baseMapper.updateByPrimaryKeySelective(param);
        if (result > 0) {
            return ResponseResult.success(1, "操作成功");
        }
        return ResponseResult.fail(2, "操作失败");
    }

    @Override
    public ResponseResult delShopVipUser(ShopVipUserDTO param) {
        Map<String,String> map = new HashMap();
        map.put("userId",param.getUserId());
        VKUserInfoVO vkUser = userVkMapper.queryVKUserInfo(map);
        if (null != vkUser) {
            return ResponseResult.fail(2,"用户已申请微客无法删除");
        }
        UserYgInfoVO shopYgUserInfo = this.baseMapper.queryYgUser(param.getUserId());
        if (null != shopYgUserInfo) {
            return ResponseResult.fail(2,"用户已申请员工无法删除");
        }
        this.baseMapper.deleteById(param.getId());
        return ResponseResult.success(1, "操作成功");
    }
}
